package com.llg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llg.mapper.LedMatchReceiveMapper;
import com.llg.mapper.ReceiveSpecMapper;
import com.llg.model.code.CommonCode;
import com.llg.model.code.PageCode;
import com.llg.model.response.*;
import com.llg.pojo.LedMatchReceive;
import com.llg.pojo.ReceiveSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.sql.RowId;
import java.util.List;

@Service
public class ReceiveSpecService {
    @Autowired
    private ReceiveSpecMapper receiveSpecMapper;
    @Autowired
    private LedMatchReceiveMapper ledMatchReceiveMapper;

    //通过品牌id查找所有接收卡
    public PageResult<ReceiveSpec> findByBrandId(int bid){
        Example example = new Example(ReceiveSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bid", bid);
        return new PageResult<>(CommonCode.SUCCESS, 1, 1,receiveSpecMapper.selectByExample(example));
    }

    //搜索并分页查询
    public PageResult<ReceiveSpec> findList(int currentPage,int pageSize,ReceiveSpec receiveSpec){
        //判断参数是否合法
        if (currentPage <= 0) return new PageResult<>(PageCode.PAGE_ERR1, 0, 0, null);
        if (pageSize <= 0) return new PageResult<>(PageCode.SIZE_ERR, 0, 0, null);
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(ReceiveSpec.class);
        Example.Criteria criteria = example.createCriteria();
        if (receiveSpec != null) {
            if (receiveSpec.getName() != null && !receiveSpec.getName().equals("")) {
                criteria.andLike("name", "%" + receiveSpec.getName() + "%");
            }
        }
        List<ReceiveSpec> receiveSpecs = receiveSpecMapper.selectByExample(example);
        PageInfo<ReceiveSpec> page = new PageInfo(receiveSpecs);
        return new PageResult<>(CommonCode.SUCCESS, (int)page.getTotal(), page.getPages(), page.getList());
    }

    //通过id查找
    public DataResult<ReceiveSpec> findById(int id){
        return new DataResult<>(CommonCode.SUCCESS, receiveSpecMapper.selectByPrimaryKey(id));
    }

    //通过led型号id查找所有未与该led关联的接收卡
    public PageResult<ReceiveSpec> findByLid(int lid){
        List<ReceiveSpec> list = receiveSpecMapper.findByLid(lid);
        return new PageResult<>(CommonCode.SUCCESS, list.size(), 1, list);
    }

    //增
    @Transactional
    public DataResult<ReceiveSpec> add(ReceiveSpec receiveSpec){
        //保存接收卡信息
        receiveSpecMapper.insertSelective(receiveSpec);
        //保存对应led带载信息
        if (receiveSpec.getReceives() != null && receiveSpec.getReceives().size() > 0) {
            for (LedMatchReceive receive : receiveSpec.getReceives()) {
                receive.setRid(receiveSpec.getId());
                ledMatchReceiveMapper.insertSelective(receive);
            }
        }
        return new DataResult<>(CommonCode.SUCCESS, receiveSpec);
    }

    //删
    public ResponseResult delete(int id){
        receiveSpecMapper.deleteByPrimaryKey(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //改
    public ResponseResult update(ReceiveSpec receiveSpec){
        receiveSpecMapper.updateByPrimaryKeySelective(receiveSpec);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
